//
//  WCTFriend.m
//  WeChatTool
//
//  Created by TerryChe on 2016/12/2.
//  Copyright © 2016年 TerryChe. All rights reserved.
//

#import "WCTFriend.h"
#import "WCTConfig.h"

@implementation WCTFriend

+ (instancetype)WCTFriendByWenober:(NSString *)wenober
                              Name:(NSString *)name
                             Photo:(NSString *)photo
                            Remark:(NSString *)remark
                              Type:(NSInteger)type
                          Uwenober:(NSString *)uwenober
//                        UpdateTime:(NSInteger)updateTime
//                       LastContent:(NSString *)lastContent
//                        LastSender:(NSInteger)lastSender
                             Label:(NSString *)label{
//    return [[self alloc]initWithWenober:wenober Name:name Photo:photo Remark:remark Type:type Uwenober:uwenober UpdateTime:updateTime LastContent:lastContent LastSender:lastSender Label:label];
    return [[self alloc]initWithWenober:wenober Name:name Photo:photo Remark:remark Type:type Uwenober:uwenober Label:label];
}

+ (instancetype)WCTFriendByDic:(NSDictionary *)dic{
    return [[self alloc]initWithWenober:dic[str_friend_wenober]
                                   Name:dic[str_friend_name]
                                  Photo:dic[str_friend_photo]
                                 Remark:dic[str_friend_remark]
                                   Type:[dic[str_friend_type] integerValue]
                               Uwenober:dic[str_friend_uwenober]
//                             UpdateTime:[dic[str_updatechatting_time] integerValue]
//                            LastContent:dic[str_last_content]
//                             LastSender:[dic[str_last_send] integerValue]
                                  Label:dic[str_friend_label]
            ];
}

- (instancetype)initWithWenober:(NSString *)wenober
                           Name:(NSString *)name
                          Photo:(NSString *)photo
                         Remark:(NSString *)remark
                           Type:(NSInteger)type
                       Uwenober:(NSString *)uwenober
//                     UpdateTime:(NSInteger)updateTime
//                    LastContent:(NSString *)lastContent
//                     LastSender:(NSInteger)lastSender
                          Label:(NSString *)label{
    if (self = [super init]) {
        self.wenober = formatEmptyString(wenober);
        self.name = formatEmptyString(name);
        self.photo = formatEmptyString(photo);
        self.remark = formatEmptyString(remark);
        self.type = type;
        self.uwenober = formatEmptyString(uwenober);
//        self.updateTime = updateTime;
//        self.lastContent = formatEmptyString(lastContent);
//        self.lastSender = lastSender;
        self.label = label;
    }
    
    return self;
}

- (NSDictionary *)convertToDic{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[WCTConfig sharedInstance].batchCount];
    
    [dic setObject:formatEmptyString(self.wenober) forKey:str_friend_wenober];
    [dic setObject:formatEmptyString(self.name) forKey:str_friend_name];
    [dic setObject:formatEmptyString(self.photo) forKey:str_friend_photo];
    [dic setObject:formatEmptyString(self.remark) forKey:str_friend_remark];
    [dic setObject:@(self.type) forKey:str_friend_type];
    [dic setObject:formatEmptyString(self.uwenober) forKey:str_friend_uwenober];
    [dic setObject:@([self checkIsDel]) forKey:str_is_del];
//    [dic setObject:@(self.updateTime) forKey:str_updatechatting_time];
//    [dic setObject:formatEmptyString(self.lastContent) forKey:str_last_content];
//    [dic setObject:@(self.lastSender) forKey:str_last_send];
    [dic setObject:formatEmptyString(self.label) forKey:str_friend_label];
    
    return dic;
}

- (int)checkIsDel{
    //标签
    if (self.type == WCT_LabelType) {
        return 3;
    }
    
////    偶数全部删除的
//    if(self.type % 2 == 0 && ![self.wenober hasSuffix:@"@chatroom"]){
//        return 2;
//    }
//
////    第四位表示自己拉黑别人，业务逻辑属于删除
//    NSArray *binaryArr = [self decimalTOBinary:self.type];
//    if (binaryArr.count >= 4 && [binaryArr[3] isEqual: @(1)]) {
//        return 1;
//    }
//
//    return 0;
    
//    if (!bean.ad_friend_wenober.contains("@chatroom")) {//群也是要上传的 需要群名称
//        String s = Integer.toBinaryString(bean.ad_relation_type);
//        if (s.endsWith("1") && !bean.ad_friend_wenober.contains("@stranger")) {//结尾不为1 或者用户名包含@stranger的 都不是好友
//            if (s.length() >= 4 && s.charAt(s.length() - 4) == '1') {//此处判断是否黑名单
//                bean.ad_isdel = 1;
//            } else {
//                bean.ad_isdel = 0;
//            }
//        } else if (s.endsWith("0") && !bean.ad_friend_wenober.contains("@stranger") && bean.ad_relation_type != 4 && bean.ad_relation_type != 6) {
//            bean.ad_isdel = 1;
//        } else {//1.结尾为1，但是包含@stranger 2.结尾为1，不包含@stranger，但type==4或6（群相关人员）
//            bean.ad_isdel = 2;
//        }
//    } else {
//        bean.ad_isdel = 0;
//    }
    if ([self.wenober hasSuffix:@"@chatroom"]) {
        return 0;
    }
    
    if (self.type % 2 == 0) {
        return 2;
    }
    
    NSArray *binaryArr = [self decimalTOBinary:self.type];
    if (binaryArr.count >= 4 && [binaryArr[3] isEqual: @(1)]) {
        return 1;
    }
    
    return 0;
}

- (NSArray *)decimalTOBinary:(uint16_t)tmpid
{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[WCTConfig sharedInstance].batchCount];
    while (tmpid)
    {
        [arr addObject:@(tmpid%2)];
        if (tmpid/2 < 1)
        {
            break;
        }
        tmpid = tmpid/2 ;
    }

    return arr;
}


- (BOOL)isEqual:(id)object {
    if([object isKindOfClass:[WCTFriend class]]){
        WCTFriend *current = (WCTFriend *)object;
        
        if([self.wenober isEqualToString:current.wenober]
           && [self.name isEqualToString:current.name]
//           && [self.photo isEqualToString:current.photo]
           && [self.remark isEqualToString:current.remark]
           && self.type == current.type
           && [self.uwenober isEqualToString:current.uwenober]
//           && self.updateTime == current.updateTime
           && [self.label isEqualToString:current.label])
        {
            return true;
        }
    }
    else if([object isKindOfClass:[NSDictionary class]]){
        NSDictionary *current = (NSDictionary *)object;
        
        if([self.wenober isEqualToString:current[str_friend_wenober]]
           && [self.name isEqualToString:current[str_friend_name]]
//           && [self.photo isEqualToString:current[str_friend_photo]]
           && [self.remark isEqualToString:current[str_friend_remark]]
           && self.type == [current[str_friend_type] integerValue]
           && [self.uwenober isEqualToString:current[str_friend_uwenober]]
//           && self.updateTime == [current[str_updatechatting_time] integerValue]
           && [self.label isEqualToString:current[str_friend_label]])
        {
            return true;
        }
    }
    
    return false;
}

//获取微信号，如果有修改后的返回修改后的。
-(NSString *)getWeChatNo{
    return [self.uwenober isEqualToString:@""] ? self.wenober : self.uwenober;
}

@end
